<extend name="common:commonTemplate"/>
<block name="main_content">
    <style>
        .container-fluid input[type="text"] {width: 50%;}
        .filter_select {
        		background-color:#efefef;
			    border: 1px solid #cdcdcd;
			    font-size: 12px;
			    height: 25px;
			    margin-top: 6px;
			    vertical-align: middle;
			    width: 100px;
		}
        .vote_item_name {
       		width:100%!important;
       		padding: 0 0 0 0 !important;
       		margin: 0 0 0 0 !important;
		}
		.filter_select::-ms-expand { display: none; }
    </style>
    <div id="content">
        <div id="content-header">
            <div id="breadcrumb">
                <a class="tip-bottom" href="index.html" data-original-title="Go to Home">
                	<i class="icon-home"></i>首页
                 </a> 
                 <a href="#">会议管理</a>
                 <a href="/vote/voteList">投票查询</a>
                 <a class="current" href="#">
           	    	<if condition="$operation == 'EDIT'">投票编辑<else />投票新增</if>
                 </a>
            </div>
        </div>
        <div class="container-fluid" id="vote_item_info">
        	<!-- 投票信息层 -->
            <div class="row-fluid">
                <div class="span12">
                    <div class="widget-box">
                        <div class="widget-title">
						<span class="icon"> <i class="icon-info-sign"></i>
						</span>
                            <h5>投票信息</h5>
                        </div>
                        <div class="widget-content nopadding">
                            <form novalidate="novalidate" id="basic_validate"
                                  name="basic_validate" action="#" method="post"
                                  class="form-horizontal">
                                <div class="control-group">
                                    <label class="control-label">投票主题<span class="required-tips">*</span></label>
                                    <div class="controls">
                                        <input type="text" id="vote_name" name="vote_name" value="{$vote.vote_name}"/>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">开始时间<span class="required-tips">*</span></label>
                                    <div class="controls">
                                    	<input id="start_time" type="text" name="start_time" value="{$vote.start_time}" class="date_controller" onclick="laydate({elem: '#start_time', istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" >
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label">结束时间<span class="required-tips">*</span></label>
                                    <div class="controls">
                                        <input id="end_time" type="text" name="end_time" value="{$vote.end_time}" class="date_controller" onclick="laydate({elem: '#end_time', istime: true, format: 'YYYY-MM-DD hh:mm:ss'})">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">最多可选择数<span class="required-tips">*</span></label>
                                    <div class="controls">
                                       <input type="text"  id="check_box_max" name="check_box_max" value="{$vote.check_box_max}" style="ime-mode:disabled"/>
                                    </div>
                                </div>
                                
                             <!--    <div class="control-group">
                                    <label class="control-label">是否匿名</label>
                                    <div class="controls">
                                    	<input type="checkbox" class="ui-wizard-content" id="is_transparent" name="is_transparent" value="{$vote.is_transparent}"/>
                                    </div>
                                </div> -->
                                
                                <div class="control-group">
                                    <label class="control-label">备注</label>
                                    <div class="controls">
                                        <textarea rows="3" cols="4" id="remark1" name="remark1"   style="width: 50%;">{$vote.remark1}</textarea>
                                    </div>
                                </div>
                                
                                <div class="control-group">
                                    <if condition="$operation == 'EDIT'">
                                    	<a id="btn_submit_vote" class="btn btn-success">更新投票</a>
                                    	<a id="btn_qr_code" style="background:red" class="btn btn-success" href="#" >二维码生成</a>
                                    	<!-- <a id="btn_report" class="btn btn-success" href="/vote/voteRecordExcel?vote_id={$vote.vote_id}" target="_blank">明细导出</a>
	                                     -->
                                    <else />
                                    	<a id="btn_submit_vote" class="btn btn-success">新增投票</a>
                                    </if>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
          <!-- 选项层 -->
       <div class="container-fluid">
           <div class="row-fluid">
               <div class="span12">
                   <div class="widget-box">
                       <div class="widget-title"><span class="icon"> <i class="icon-th"></i> </span>
                          <h5>投票项信息</h5>
                          <span style="float: right;margin: 9px 11px 0 0;">
                           		<a id="btn_save_item" class="btn btn-mini btn-primary">保存</a>
                           		<a id="btn_reset_item" class="btn btn-mini btn-info">重置</a>
                          </span>
                          <span style="float: right;margin: float: right;margin: 8px 8px 0 0;">
                         	 <input type="text" id="save_item_name" placeholder="请输入选项名称" style="width:200px;height:22px;padding: 0 0 0 0 !important;margin: 0 0 0 0 !important;" />
                          </span>
                          <span class="label label-important" id="update_label" style="float:right;margin: 10px 8px 0 0;display: none;">修改</span>
                          <span class="label label-success" id="add_label" style="float:right;margin: 10px 8px 0 0;">新增</span>
                       </div>
                       <div class="widget-content nopadding">
                           <table id="search_result" page_index="1" class="table table-bordered table-striped">
                               <thead>
                                <tr>
                                    <th style="vertical-align: middle;">名称</th>
                                    <th style="vertical-align: middle;">票数</th>
                                    <th style="vertical-align: middle;width: 50px;">操作</th>
                                </tr>
                               </thead>
                               <tbody></tbody>
                           </table>
                       </div>
                   </div>
               </div>
           </div>
          </div>
      </div>
</block>
<block name="js_content">
    <script src="/Public/js/ajaxService.js?v=12" type="text/javascript"></script>
    <script src="/Public/js/common.js?v=16" type="text/javascript"></script>
    <script src="/Public/js/laydate/laydate.js" type="text/javascript"></script> 
    <script src="/Public/assets/js/layer/layer.js" type="text/javascript"></script>
    <script>
        $(function(){
            VotePage = {
                'Init': function () {
                	VotePage.EDIT_ITEM_ID = null;
                	VotePage.EDIT_ITEMS = {};
                	VotePage.DEL_ITEM_LIST = new Array;
                	//校验
                    $("#basic_validate").validate({
                		rules:{
                			vote_name:{
                				required:true
                			},
                			start_time:{
                				required:true
                			},
                			end_time:{
                				required:true
                			},
                			check_box_max:{
                				number:true,
                				required:true
                			}
                		},
                		errorClass: "help-inline",
                		errorElement: "span",
                		highlight:function(element, errorClass, validClass) {
                			$(element).parents('.control-group').addClass('error');
                		},
                		unhighlight: function(element, errorClass, validClass) {
                			$(element).parents('.control-group').removeClass('error');
                			$(element).parents('.control-group').addClass('success');
                		}
                	});
                	//给予新增/更新投票按钮绑定事件
                    $('#btn_submit_vote').bind('click', function () {
                    	var valid_result = $('#basic_validate').valid();
                    	if(valid_result){
	                    	var param = {};
	                    	param.vote_name = $('#vote_name').val();
	                    	param.start_time = $('#start_time').val();
	                    	param.end_time = $('#end_time').val();
	                    	param.remark1 = $('#remark1').val();
	                    	param.check_box_max = $('#check_box_max').val();
	                    	param.vote_id = "{$vote.vote_id}";
	                    	param.edit_items = new Array();
	                    	param.add_items = new Array();
	                    	$.each(VotePage.EDIT_ITEMS, function(i, item){
	                    		if(item.operation == 'ADD'){
	                    			param.add_items.push(item);
	                    		}else if(item.operation == 'EDIT'){
	                    			param.edit_items.push(item);
	                    		}
                    		});
	                    	param.del_items = VotePage.DEL_ITEM_LIST;
	                    	$.ajax({
	                            type: 'post',
	                            url: '/AjaxService/center/vote/saveorupdate',
	                            data: param,
	                            dataType: 'json',
	                            async: true,
	                            success: function (data) {
	                                if (data.IsSuccess == 0) {
	                                	layer.msg(data.ErrorMsg, {icon: 5});
	                                } else {
	                                	layer.msg('操作成功', {icon: 0});
	                                	VotePage.LoadItems(1);
	                                }
	                            },
	                            error: function (e, err_name, err_text) {
	                            	layer.msg(err_text, {icon: 5});
	                            }
	                        });
                    	}
                    });
                    //二维码生成
                   	 $('#btn_qr_code').bind('click', function () {
                   		VotePage.showQrCode("{$vote.vote_id}");
                    }); 
                    //重置
                   	$('#btn_reset_item').bind('click', function () {
                       	VotePage.ResetItem();
                    });
                    //追加选项
                   	$('#btn_save_item').bind('click', function () {
                   		var item_name = $('#save_item_name').val();
                   		if(CommonPage.convertNull(item_name) == ''){
                   			layer.msg('选项名称不能为空', {icon: 5});
                   			return
                   		}
                   		//新增
                   		if(VotePage.EDIT_ITEM_ID == null){
	                   		var timestamp = new Date().getTime();
	                        $('#search_result tbody').append(VotePage.BuildHtml(timestamp,item_name,0));
	                        $('.tip-top').tooltip({ placement: 'top' });
	                        VotePage.EDIT_ITEMS[timestamp] = {item_name:item_name,item_count:0,operation:'ADD'};
                   		}else{
                   			//编辑
                   			var data = VotePage.EDIT_ITEMS[VotePage.EDIT_ITEM_ID];
                   			data.item_name = item_name;
                   			$('#item_name_'+VotePage.EDIT_ITEM_ID).html(item_name);
                   			if(data.operation != 'ADD'){
                   				data.operation = 'EDIT'
                   			}
                   			VotePage.EDIT_ITEMS[VotePage.EDIT_ITEM_ID] = data;
                   			VotePage.ResetItem();
                   		}
                    });
                    
                },
                'BuildHtml' : function(item_id,item_name,item_count){
                	var html = '';
                    html += "<tr id='"+ item_id +"'>";
                    html += "<td id='item_name_" +item_id +"'>" + CommonPage.convertNull(item_name) + "</td>";
                    html += "<td>" + item_count + "</td>"
                    html += "<td  style='text-align: center;'>";
                    html += '<a data-original-title="编辑" class="tip-top" href="javascript:void(0);" onclick="VotePage.EditItem('+ '\'' +item_id + '\'' +')" ><i class="icon-pencil"></i></a>&nbsp;&nbsp;';
                    html += '<a data-original-title="删除" class="tip-top" href="javascript:void(0);" onclick="VotePage.RemoveItem('+ '\'' + item_id + '\'' +')" ><i class="icon-remove"></i></a>';
                    html += "</td>";
                    html += "</tr>";
                    return html;
                },
                'RemoveItem' : function(id){
                	$('#'+id).remove();
                	//获取数据缓存
                	var data = VotePage.EDIT_ITEMS[id];
					if(data.operation != 'ADD'){
                		data.operation = 'DEL';
                		VotePage.DEL_ITEM_LIST.push(id);
                	}
                	//删除
                	delete VotePage.EDIT_ITEMS[id];
                },
                'EditItem' : function(id){
                	var data = VotePage.EDIT_ITEMS[id];
                	$('#save_item_name').val(data.item_name);
                	VotePage.EDIT_ITEM_ID = id; 
                	$('#add_label').hide();
                	$('#update_label').fadeIn("slow");
                },
                'ResetItem' : function(){
                	$('#save_item_name').val('');
                   	$('#update_label').hide();
                   	$('#add_label').fadeIn("slow");
                   	VotePage.EDIT_ITEM_ID = null;
                },
                'LoadItems': function(page_index){
                	//投票CODE为空时 初始化不查询联系人 构造空的查询结果页面
                	if(CommonPage.convertNull("{$vote.vote_id}") == ''){
                		 VotePage.fillItemTable([]);
                	}else{
                		var param = {};
                    	param.vote_id = "{$vote.vote_id}";
	                    $.ajax({
	                        type: 'post',
	                        url: '/AjaxService/center/vote/queryitemsbyvoteid',
	                        data: param,
	                        dataType: 'json',
	                        async: true,
	                        success: function (data) {
	                            VotePage.fillItemTable(data.Result);
	                        },
                            error: function (e, err_name, err_text) {
                            	layer.msg(err_text, {icon: 5});
                            }
	                    });
                	}
                },
                'fillItemTable': function(lst_item){
                	VotePage.EDIT_ITEMS = {};
                	VotePage.DEL_ITEM_LIST = new Array;
                	VotePage.ResetItem();
                	var html = '';
                    if (lst_item.length != 0) {
                        for (var index in lst_item) {
                            var item_en = lst_item[index];
                            html += (VotePage.BuildHtml(item_en.item_id,item_en.item_name,item_en.item_count));
	                        item_en.operation = 'INIT';
	                        VotePage.EDIT_ITEMS[item_en.item_id] = item_en;
                        }
                    }
                    $('#search_result tbody').html('');
                    $('#search_result tbody').append(html);
                    $('.tip-top').tooltip({ placement: 'top' });
                },
                'showQrCode' : function(vote_id){
                    layer.open({
                        type: 2,
                        title: '投票二维码',
                        fix: true,
                        shadeClose: true,
                        maxmin: false,
                        area: ['380px', '410px'],
                        content: '/vote/voteQrCode?vote_id='+vote_id
                    });
                },
        		'ConvertOptions': function(type,obj){
        			var display = '【转换失败:'+obj+'】';
        			$.each(VotePage[type], function(key, value){
        			  if(key == obj){
        				  display = value;
        				  return false;
        			  }
        			});
        			return display;
        		}
            };
            $(document).ready(function () {
                VotePage.Init();
                VotePage.LoadItems(1);
            });
        });
    </script>
</block>